package superminer.xinfang.com.ipreport.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import superminer.xinfang.com.ipreport.R;

import java.util.List;

/**
 * Created by Administrator on 2018/4/2.
 */

public class SelectDialog extends Dialog {


    private TextView tv_quit;//取消键
    private TextView tv_sure;//确定
    private TextView tv_title;//标题
    private ListView listView;
    private List<String> list;

    private OnSelectListener onSelectListener;

    private SelectDialogAdapter selectDialogAdapter;
    private String title;
    private Context context;
    public SelectDialog(Context context, List<String> list, OnSelectListener onSelectListener,String title) {
        super(context, R.style.mydialog);
        this.list = list;
        this.context=context;
        this.onSelectListener = onSelectListener;
        this.title=title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_select);
        initView();
    }

    private void initView() {
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText(title);
        tv_quit = findViewById(R.id.tv_quit);
        tv_sure = findViewById(R.id.tv_sure);
        listView = findViewById(R.id.listview);

        selectDialogAdapter = new SelectDialogAdapter(context, list);
        listView.setAdapter(selectDialogAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                selectDialogAdapter.setPos(i);
                selectDialogAdapter.notifyDataSetChanged();
            }
        });
        tv_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                //取消
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //确定
                onSelectListener.getSelect(list.get(selectDialogAdapter.getPos()));
                dismiss();
            }
        });
    }

    /**
     * 回调选择项
     */
    public interface OnSelectListener
    {
        void getSelect(String s);
    }


     class SelectDialogAdapter extends BaseAdapter{
         private List<String> list;
         private Context context;
         private int pos=0;//选中

         public SelectDialogAdapter(Context context, List<String> list) {
             this.context = context;
             this.list = list;
         }

         @Override
         public int getCount() {
             return list.size();
         }

         @Override
         public Object getItem(int i) {
             return list.get(i);
         }

         @Override
         public long getItemId(int i) {
             return i;
         }

         @Override
         public View getView(int i, View view, ViewGroup viewGroup) {
             view= LayoutInflater.from(context).inflate(R.layout.item_select_dialog,null);
             TextView tv_tel=view.findViewById(R.id.tv_tel);

             tv_tel.setText(list.get(i));
             if (i==pos)
             {
                 tv_tel.setTextColor(context.getResources().getColor(R.color.system_color));
             }else
             {
                 tv_tel.setTextColor(context.getResources().getColor(R.color.gray));
             }
             return view;
         }

         public int getPos() {
             return pos;
         }

         public void setPos(int pos) {
             this.pos = pos;
         }
     }
}
